namespace NUnitTests{
    using System;
    using System.IO;
    using Guia.Core;
    using Guia.Elements;
    using NUnit.Framework;
    using WpfTestApplication;

    /// <summary>
    ///</summary>
    [TestFixture()]
    public class OpenFileDialogTest : AbstractWindowTest
    {
        protected override Type TestWindowType()
        {
            return typeof(OpenFileDialogTestWindow);
        }

        protected override string TestWindowName()
        {
            return "OpenFileDialogTestWindow";
        }

        /// <summary>
        /// Use TestInitialize to run code before running each test 
        /// </summary>
        [SetUp()]
        public void MyTestInitializeFiles()
        {
            File.WriteAllText("test1.txt", "foo");
            File.WriteAllText("test2.txt", "bar");
        }

        [Test]
        public void TestOpenFile()
        {
            Assert.IsNotNull(window);
            window.Get<Button>("btnOpenFile").Invoke();
            OpenFileDialog openDialog = window.GetOpenFileDialog();
            openDialog.Open("test1.txt");
            Assert.AreEqual("foo", window.Get<TextBox>("txtOpenFile").Value);
        }

        [Test]
        public void TestOpenFileTitle()
        {
            Assert.IsNotNull(window);
            window.Get<Button>("btnOpenFileTitle").Invoke();
            OpenFileDialog openDialog = window.GetOpenFileDialog("Open my file");
            openDialog.Open("test1.txt");
            Assert.AreEqual("foo", window.Get<TextBox>("txtOpenFile").Value);
        }

        [Test]
        public void TestOpenFileWrongTitle()
        {
            Assert.IsNotNull(window);
            window.Get<Button>("btnOpenFileTitle").Invoke();
            OpenFileDialog openDialog = null;
            try
            {
                openDialog = window.GetOpenFileDialog();
                Assert.Fail();
            } 
            catch (ElementNotFoundException)
            {
                Assert.IsTrue(true);
            } 
            finally
            {
                openDialog = window.GetOpenFileDialog("Open my file");
                openDialog.Cancel();
            }
        }

        [Test]
        public void TestOpenFileCancel()
        {
            Assert.IsNotNull(window);
            window.Get<Button>("btnOpenFile").Invoke();
            OpenFileDialog openDialog = window.GetOpenFileDialog();
            openDialog.Cancel();
            Assert.AreEqual("", window.Get<TextBox>("txtOpenFile").Value);
        }

        [Test]
        public void TestOpenMultipleFiles()
        {
            Assert.IsNotNull(window);
            window.Get<Button>("btnOpenFiles").Invoke();
            OpenFileDialog openDialog = window.GetOpenFileDialog();
            openDialog.Open(new string[] { "test1.txt", "test2.txt" });
            Assert.AreEqual("foobar", window.Get<TextBox>("txtOpenFiles").Value);
        }
    }
}
